package trafficsimulator;

import java.util.Arrays;

public class Car
{

    private char carDesignator;
    private char direction;
    private char[] route;
    private int speed;
    private int destX;
    private int destY;
    private int currentX;
    private int currentY;
    private int previousX;
    private int previousY;
    private int stepCount;
    private int originX;
    private int originY;

    public Car(int speed, char direction, int startX, int startY, int destX, int destY, char designator)
    {
        this.originX = startX;
        this.originY = startY;
        this.destX = destX;
        this.destY = destY;
        this.currentX = startX;
        this.currentY = startY;
        this.speed = speed;
        this.stepCount = 0;

        if (direction == 'N' || direction == 'S' || direction == 'E' || direction == 'W')
        {
            this.direction = direction;
        } else
        {
            throw new IllegalArgumentException("Car(char direction): Direction must be uppercase N,S,E or W.");
        }

        if (designator != '1' || designator != '0')
        {
            this.carDesignator = designator;
        } else
        {
            throw new IllegalArgumentException("Car(): Designator must not be 1 or 0.");
        }
    }

    public void step()
    {
        this.previousX = this.currentX;
        this.previousY = this.currentY;
        this.direction = route[stepCount];
        if (this.direction == 'N')
        {
            currentY = currentY - speed;
        } else if (this.direction == 'S')
        {
            currentY = currentY + speed;
        } else if (this.direction == 'E')
        {
            currentX = currentX + speed;
        } else if (this.direction == 'W')
        {
            currentX = currentX - speed;
        }
        stepCount++;
    }

    public int getX()
    {
        return this.currentX;
    }

    public int getY()
    {
        return this.currentY;
    }

    public boolean isCrashed(Car c)
    {
        return this.getX() == c.getX() && this.getY() == c.getY();
    }

    public boolean hasArrived()
    {
        return this.getX() == this.destX && this.getY() == this.destY;
    }

    public char getDesignator()
    {
        return this.carDesignator;
    }

    public char getDirection()
    {
        return this.direction;
    }

    public int getSpeed()
    {
        return this.speed;
    }

    public int getPreviousX()
    {
        return this.previousX;
    }

    public int getPreviousY()
    {
        return this.previousY;
    }

    public void createRoute(Map m)
    {
        int xPos = this.currentX;
        int yPos = this.currentY;
        int routeCounter = 0;
        route = new char[100];
        Arrays.fill(route, '0');
        while (xPos != this.destX)
        {
            if (xPos < this.destX)
            {
                route[routeCounter] = 'E';
                xPos++;
                routeCounter++;
            }
            else if (xPos > this.destX)
            {
                route[routeCounter] = 'W';
                xPos--;
                routeCounter++;
            }
        }
        while(m.getNext('N', xPos, yPos)=='*')
        {
            route[routeCounter] = 'E';
            routeCounter++;
            xPos++;
        }

        while (yPos != this.destY)
        {
            if (yPos < this.destY)
            {
                    route[routeCounter] = 'S';
                    routeCounter++;
                    yPos++;
            } 
            else if (yPos > this.destY)
            {
                    route[routeCounter] = 'N';
                    routeCounter++;
                    yPos--;
            }
        }
        while (xPos != this.destX)
        {
            if (xPos < this.destX)
            {
                route[routeCounter] = 'E';
                xPos++;
                routeCounter++;
            }
            else if (xPos > this.destX)
            {
                route[routeCounter] = 'W';
                xPos--;
                routeCounter++;
            }
        }
    }

    public char[] getRoute()
    {
        return this.route;
    }

    public int getDestX()
    {
        return this.destX;
    }

    public int getDestY()
    {
        return this.destY;
    }
}
